package LeetCode.linkedlist;

public class ReverseLinkedList {
    //链表反转，最核心的点就是，设置三个结点变量，首先把头节点赋值给curr，pre为null，temp存储下个结点

    public static ListNode createlistnode(){
        ListNode head = new ListNode(0);
        ListNode listnode = head;
        for(int i=1;i<4;i++){
            listnode.next = new ListNode(i);
            //System.out.println(listnode.var);
            listnode = listnode.next;
        }
        return head;
    }

    public static void shownode(ListNode node){
        do {
            System.out.println(node.var);
            node = node.next;
        }while (node!=null);
    }

    public static ListNode reverse(ListNode head){
        ListNode prev = null;
        ListNode curr = head;
        ListNode nextTmp;
        while(curr != null){
            nextTmp = curr.next;
            curr.next = prev;
            prev = curr;
            curr = nextTmp;
        }

        return prev;
    }

    public static void main(String[] args) {
        ListNode head = ReverseLinkedList.createlistnode();
        ReverseLinkedList.shownode(head);
        ListNode prev = ReverseLinkedList.reverse(head);
        System.out.println("after reverse");
        ReverseLinkedList.shownode(prev);
    }
}
